package com.fyl.pattern.singleton.demo8;

import com.fyl.pattern.singleton.demo8.Singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 测试使用反射破坏单例模式
 */
public class Client {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //1.获取Singleton的字节码对象
        Class<Singleton> clazz = Singleton.class;
        //2.获取无参构造方法对象
        Constructor<Singleton> cons = clazz.getDeclaredConstructor();
        //3.取消访问检查
        cons.setAccessible(true);
        //创建singleton对象
        Singleton s1 = cons.newInstance();
        Singleton s2 = cons.newInstance();
        System.out.println(s1 == s2); //如果返回的是true说明没有破坏单例模式
    }
}
